//
//  YTTMeController.m
//  跑圈儿
//
//  Created by 唐康 on 16/4/19.
//  Copyright © 2016年 唐康. All rights reserved.
//

#import "YTTMeController.h"
#import "YTTDataTableViewCell.h"

@interface YTTMeController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;

@property(nonatomic, strong)NSArray *dataArr;

@property(nonatomic, strong)UIImageView *img;

@end

@implementation YTTMeController

-(void) viewWillAppear:(BOOL)animated {
    
    [[YTTDataManager shareInstance] openDB];
    self.dataArr = [[YTTDataManager shareInstance] selectAll];
    [self.tableView reloadData];
    
    self.navigationController.navigationBar.hidden = YES;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
       [self creatTablewView];
    
   
    
}




-(void)creatTablewView{
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 90;
    self.tableView.separatorStyle = NO;                 //隐藏cell之间的分割线
    [self.tableView registerClass:[YTTDataTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.contentInset = UIEdgeInsetsMake(180, 0, 0, 0);
    
    self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, YTTScreenW, 140)];
    self.img.image =[UIImage imageNamed:@"Running"];
    self.img.contentMode = UIViewContentModeScaleAspectFill;
    [self.tableView addSubview:self.img];
    self.img.tag = 1001;
    
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YTTDataTableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSMutableArray *arr = [[YTTDataManager shareInstance] selectAll];
    
    NSDictionary *dic = arr[indexPath.row];
    NSLog(@"字典  %@", dic);
    
    cell.img.image = [UIImage imageNamed:   @"run_map.png"];
    
    cell.timerL.text = dic[@"time"];
    cell.timerL.textColor = [UIColor colorWithRed:76 / 255.0 green:145 / 255.0 blue:235 / 255.0 alpha:0.9];
    cell.timerL.font = [UIFont systemFontOfSize:19 weight:0.3];
    cell.timerL.textAlignment = NSTextAlignmentCenter;
    
    cell.dataLable.text = dic[@"calendar"];
    cell.dataLable.textAlignment = NSTextAlignmentCenter;
    cell.dataLable.textColor = [UIColor lightGrayColor];
    
    NSString *str = dic[@"miter"];
    NSString *strRange = [str substringToIndex:5];
    NSString *kiloStr = [NSString stringWithFormat:@"%@km",strRange];
    cell.miterL.text = kiloStr;
    
    cell.miterL.textColor = [UIColor colorWithRed:76 / 255.0 green:145 / 255.0 blue:235 / 255.0 alpha:0.9];
    cell.miterL.font = [UIFont systemFontOfSize:19];
    
    return  cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //点击过后取消选中状态
    [self.tableView deselectRowAtIndexPath: indexPath animated:YES];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGPoint offset = scrollView.contentOffset;
    if (offset.y < 140) {
        CGRect rect = [self.tableView viewWithTag:1001].frame;
        rect.origin.y = offset.y;
        rect.size.height = -offset.y;
        [self.tableView viewWithTag:1001].frame = rect;
    }
    
    
    
}































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
